<template>
    <view>
        <view>
            <button style="margin-top: 20px" type="primary" @tap="login">测试Login接口</button>
            <button style="margin-top: 20px" type="default" @tap="go">调用test GO接口</button>
            <button style="margin-top: 20px" type="primary" @tap="wxLogin">调用微信用户授权登录</button>
            <button style="margin-top: 20px" lang="zh_CN" type="warn" @tap="getUserProfile">授权获取用户信息</button>
            <button style="margin-top: 20px" open-type="getPhoneNumber" lang="zh_CN" type="primaray" @getphonenumber="onGotPhoneNumber">获取手机号</button>
            <button style="margin-top: 20px" type="primary" @tap="gotoBindSystemUser">测试绑定系统用户</button>
            <button style="margin-top: 20px" type="primary" @tap="gotoLoginSystemUser">使用系统用户登录</button>

            <button style="margin-top: 20px" type="primary" open-type="contact">进入客服</button>
            <button style="margin-top: 20px" type="warn" @tap="getMyWecahtUserInfo">获取当前用户信息</button>
            <button style="margin-top: 20px" type="warn" @tap="wxUploadFile">文件上传演示</button>
        </view>

        <button style="margin-top: 20px" type="warn" @tap="intoMyInfoPage">进入个人信息</button>
        <button style="margin-top: 20px" type="primary" @tap="intoPayDemo">微信支付</button>
    </view>
</template>

<script>
//获取应用实例
const app = getApp();
const JBolt = app.globalData.getJBolt();
export default {
    data() {
        return {};
    },
  onLoad: function () {
      JBolt.log('1、app onlaunch');
      JBolt.init();
        // this.readData();
    },
    methods: {
        readData: function () {
            JBolt.log('readData');
            JBolt.apiPost({
                url: JBolt.API.test.go,
                loading: true,
                data: {
                    id: 1
                }
            }).then(() => {
                JBolt.log('readData finished');
            });
        },

        login: function () {
            JBolt.apiPost({
                url: JBolt.API.test.login,
                loading: true,
                data: {
                    id: 1
                }
            }).then(() => {
                JBolt.openPage('/pages/jbolt/user/ucenter/index');
            });
        },

        go: function () {
            JBolt.apiPost({
                url: JBolt.API.test.go,
                loading: true
            }).then(() => {
                JBolt.openPage('/pages/jbolt/user/ucenter/index');
            });
        },

        wxLogin: function (e) {
            JBolt.WxLoginUtil.login().then(() => {
                JBolt.log('第1次运行Login');
            });
            JBolt.WxLoginUtil.login().then(() => {
                JBolt.log('第2次运行Login');
            });
            JBolt.WxLoginUtil.login().then(() => {
                JBolt.log('第3次运行Login');
            });
            JBolt.WxLoginUtil.login().then(() => {
                JBolt.log('第4次运行Login 并且跳转页面');
                JBolt.openPage('/pages/jbolt/user/ucenter/index');
            });
        },

        getUserProfile: function () {
            JBolt.getUserProfile(function (res) {
                JBolt.decryptUserInfo(res)
                    .then(() => {
                        JBolt.openPage('/pages/jbolt/user/ucenter/index');
                    })
                    .catch(() => {
                        JBolt.openPage('/pages/jbolt/user/login/index');
                    });
            });
        },

        gotoBindSystemUser: function () {
            JBolt.openPage('/pages/jbolt/user/bind/index');
        },

        gotoLoginSystemUser: function () {
            JBolt.openPage('/pages/jbolt/user/syslogin/index');
        },

        onGotPhoneNumber: function (e) {
            JBolt.log(e);
            JBolt.decryptPhoneNumber(e.detail)
                .then((data) => {
                    JBolt.log('手机号信息:' + JSON.stringify(data));
                    JBolt.openPage('/pages/jbolt/user/ucenter/index');
                })
                .catch(() => {
                    JBolt.openPage('/pages/jbolt/user/login/index');
                });
        },

        getMyWecahtUserInfo: function () {
            JBolt.apiGet({
                url: JBolt.API.wechat.user.me
            }).then((data) => {
                JBolt.log('记过');
                JBolt.log(data);
            });
        },

        wxUploadFile: function () {
            //选择文件 调用上传接口
            uni.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],

                success(res) {
                    // tempFilePath可以作为img标签的src属性显示图片
                    const file = res.tempFilePaths[0];
                    JBolt.uploadFile({
                        url: JBolt.API.test.uploadImage,
                        file: file
                    }).then((res) => {
                        JBolt.log('上传成功后的回调函数执行');
                        JBolt.log(res);
                    });
                }
            });
        },

        intoMyInfoPage: function () {
            JBolt.openPage('/pages/jbolt/user/ucenter/index');
        },

        intoPayDemo: function () {
            JBolt.openPage('/pages/jbolt/pay/index');
        }
    }
};
</script>
<style>
@import './index.css';
</style>
